package com.geekteam.masterenglish.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import android.content.Context;

import com.google.gson.Gson;

public class JsonUtil {
	
	public static String toJson(Object obj) {
		return new Gson().toJson(obj);
	}
	public static Object fromJson(String json, Class<?> X) {
		return new Gson().fromJson(json, X);
	}
	
	public static String loadResourceJson(Context context, int resource) throws IOException {
        InputStream is = context.getResources().openRawResource(resource);
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }

        return writer.toString();
    }
}
